<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
        <div class="container">
            <h4 style="margin-bottom: 20px">{{form.breedName}}</h4>
			<el-row>
                <el-form-item label="品名：">{{form.breedName}}</el-form-item>
				<el-form-item label="生产批号：">{{form.batch}}</el-form-item>
			</el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="产品大类：">
                        {{form.inventoryType}}
                    </el-form-item>
                    <el-form-item label="发药机编码：">
                        {{form.dispenserCode}}
                    </el-form-item>
                    <el-form-item label="生产日期：">
                        {{form.time}}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="存货代码：">
                        {{form.inventoryCode}}
                    </el-form-item>
					<el-form-item label="装量：">
					    {{form.materialBreed.pelletWeight}}{{form.materialBreed.loadingUnits}}
					</el-form-item>
                    <el-form-item label="保质期：">
                        {{form.deadLine}}月
                    </el-form-item>
                </el-col>
            </el-row>
        </div>
        <div v-if="showForm">
            <div class="container" style="margin-top:20px">
                <h3 style="margin-bottom: 20px">分包</h3>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="投料量：">
                            {{form.scUseNum}}{{form.scUseNumUnits}}
                        </el-form-item>
                        <el-form-item label="每包装量：">
                            {{ruleForm.weight}}{{form.loadingUnits}}
                        </el-form-item>
                        <!-- <el-form-item label="生产指令：">
                            {{ruleForm.inBatch}}
                        </el-form-item> -->
                        <!-- <el-form-item label="总计量：">
                            {{totalWeight}}{{form.units}}
                            <span v-if="showBreed">相当于饮片{{form.materialBreed.ypQuantity}}{{form.materialBreed.ypUnits}}</span>
                        </el-form-item> -->
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="分包数量：">
                            {{ruleForm.printNum}}包
                        </el-form-item>
                    </el-col>
                </el-row>
            </div>
            <el-form-item class="footerFixed">
                <el-button type="primary" @click="submitForm">确认无误</el-button>
                <el-button @click="showForm = false">返回修改</el-button>
            </el-form-item>
        </div>
        <div v-else>
            <div class="container" style="margin-top:20px">
                <h3 style="margin-bottom: 20px">分包</h3>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="投料量：">
                            {{form.scUseNum}}{{form.scUseNumUnits}}
                        </el-form-item>
                        <el-form-item label="每包装量：" prop="weight">
                            <el-input v-model.number="ruleForm.weight" type="number" :disabled="isDisabled" style="width: 200px"></el-input>
                            <span>&nbsp;&nbsp;{{form.loadingUnits}}</span>
                        </el-form-item>
                        <!-- <el-form-item label="总计量：">
                            {{totalWeight}}{{form.units}}
                            <span v-if="showBreed">相当于饮片{{form.materialBreed.ypQuantity}}{{form.materialBreed.ypUnits}}</span>
                        </el-form-item> -->
                    </el-col>
                    <el-col :span="12">
                        <el-form-item>
                            &nbsp;
                        </el-form-item>
                        <el-form-item label="分包数量：" prop="printNum">
                            <el-input v-model="ruleForm.printNum" @change="changeNum" @blur="blurNum1" style="width: 200px"></el-input>
                            <span>&nbsp;&nbsp;包</span>
                        </el-form-item>
                    </el-col>
                </el-row>
            </div>
            <el-form-item class="footerFixed">
                <el-button type="primary" @click="submitRules('ruleForm')">提交</el-button>
                <el-button @click="backPage">返回</el-button>
            </el-form-item>
        </div>
        </el-form>
    </div>
</template>

<script>
import {print_save,pack_findById} from "@/request/api";
    export default {
        components: {
        },
        data() {
            return {
                showForm: false,
                showBreed: false,
                isDisabled: true,
                form: {
                    materialBreed: {
                        ypQuantity: ''
                    }
                },
                // totalWeight: 0,
                weight: '',
                printNum: 0,
                ruleForm: {
                    printNum: 0,
                    weight: 0,
                    printSpec: '',
                    units: '',
                    particlePackingId: '',
                    materialType: '',

                },
                rules: {
                    printNum: [
                        { required: true, message: '不能为0', trigger: 'blur' },
                    ],
                    weight: [
                        { required: true, message: '不能为0', trigger: 'change' }
                    ],
                }
            };
        },
        mounted(){
            this.id = this.$route.query.id
            this.init()
        },
        methods: {
            backPage () {
                this.$router.go(-1);
            },
            init() {
                pack_findById(this.id).then((res) =>{
                    this.form = res.data
                    console.log(res);
                    this.ruleForm.weight = res.data.materialBreed.pelletWeight
                    // console.log(Number(this.ruleForm.weight))
                    this.form.loadingUnits = res.data.materialBreed.loadingUnits
                    var totalNum = 0
                    if (res.data.scUseNumUnits == 'kg') {
                        totalNum = Number(this.form.scUseNum)*1000
                        this.printNum = totalNum/Number(this.ruleForm.weight)
                        //  var num = this.printNum*0.1
                        // // console.log(num)
                        // if(num >1000){
                        //     num = 1000
                        // }
                        var num = 0
                        if(this.printNum>10000){
                          num = this.printNum*0.3
                        }else if(this.printNum < 10000){
                          num = this.printNum*0.5
                        }


                        this.printNum = this.printNum + num
                        // this.ruleForm.printNum = totalNum/Number(this.ruleForm.weight)
                        this.ruleForm.printNum = Math.ceil(this.printNum)
                    } else if (res.data.scUseNumUnits == 'g') {
                        totalNum = Number(this.form.scUseNum)

                        this.printNum = totalNum/Number(this.ruleForm.weight)
                        //  var num = this.printNum*0.1
                        //   // console.log(num)
                        // if(num >1000){
                        //     num = 1000
                        // }
                      var num = 0
                      if(this.printNum>10000){
                        num = this.printNum*0.3
                      }else if(this.printNum < 10000){
                        num = this.printNum*0.5
                      }
                        this.printNum = this.printNum + num
                        // this.ruleForm.printNum = totalNum/Number(this.ruleForm.weight)
                        this.ruleForm.printNum = Math.ceil(this.printNum)
                    }
                    // if (res.data.materialBreed != null) {
                        // if (this.form.materialType == '配方颗粒' && this.form.materialBreed.ypQuantity != '') {
                        //     this.showBreed = true
                        // } else {
                        //     this.form.materialBreed.ypQuantity = ''
                        //     this.showBreed = false
                        // }
                    // }
                })
            },
            changeNum(val){
                // this.ruleForm.weight = Math.floor((this.form.scUseNum/val)*100) / 100
                // this.weight = Math.floor((this.form.scUseNum/val)*100) / 100









                // this.isDisabled = false
                if (this.ruleForm.printNum <= 0) {
                    this.$notify.error({
                        title: '提示',
                        message: '数量不能为零或负数！',
                        showClose: false,
                        duration:2000
                    });
                    this.ruleForm.printNum = this.printNum
                }
                if (this.ruleForm.printNum > this.printNum) {
                    this.$notify.error({
                        title: '提示',
                        message: '不能大于最多分包数！',
                        showClose: false,
                        duration:2000
                    });
                    this.ruleForm.printNum = this.printNum
                }
            },
            // changeNum1(val){
            //     if (this.ruleForm.weight <= 0) {
            //         this.$notify.error({
            //             title: '提示',
            //             message: '数量不能为零或负数！',
            //             showClose: false,
            //             duration:2000
            //         });
            //     }
            //     var tolNum = this.ruleForm.printNum*this.ruleForm.weight
            //     if (tolNum > this.form.scUseNum) {
            //         this.$notify.error({
            //             title: '提示',
            //             message: '总计量不能大于投料量！',
            //             showClose: false,
            //             duration:2000
            //         });
            //         this.ruleForm.weight = this.weight
            //     }
            // },
            blurNum1(){
                // this.totalWeight = this.ruleForm.printNum*this.ruleForm.weight
            },
            // blurNum2(val){
            //     this.totalWeight = this.ruleForm.printNum*this.ruleForm.weight
            // },
            submitRules(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.showForm = true
                        this.ruleForm.scUseNum = this.form.scUseNum;
                        this.ruleForm.units = this.form.loadingUnits;
                        this.ruleForm.particlePackingId = this.id;
                        this.ruleForm.materialType = this.form.materialType;
                        // console.log(this.ruleForm,111)
                    } else {
                        return false;
                    }
                })
            },
            submitForm() {
                // if (this.form.materialType == '配方颗粒') {
                //     this.ruleForm.units = this.form.materialBreed.ypUnits
                // }
                // this.ruleForm.printSpec = this.form.units+'/包';
                // this.ruleForm.time = this.form.time
				/* this.ruleForm.deadLine = this.form.deadLine */
                print_save(this.ruleForm).then((res) => {
                    if (res.code == 200) {
                        this.$notify({
                            title: '提示',
                            message: '分包成功',
                            type: 'success',
                            duration:1000
                        });
                        this.backPage()
                    } else{
                        this.$notify.error({
                            title: '提示',
                            message: '分包失败！',
                            showClose: false,
                            duration:1000
                        });
                    }
                })
            },
        }
    }
</script>
<style scoped lang="less">

</style>
